package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;

public class CnhubeiNewsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		// http://jr.cnhubei.com/hbjr/201211/t20121121_19428.shtml
		// http://wshb.cnhubei.com/NewPage.asp?id=5512
		//http://edu.cnhubei.com/?action-viewnews-itemid-63233
		//http://travel.cnhubei.com/portal.php?mod=view&aid=4183
		//http://xy.cnhubei.com/2012/1123/29031.shtml
		//http://yc.cnhubei.com/html/hcpzj/20121121-16795.html
		String[] regex={
				"http://.*[.]cnhubei[.]com/.*/[0-9]{6}/t[0-9]*[_]{0,1}[0-9]*[.]shtml",
				"http://wshb[.]cnhubei[.]com/NewPage[.]asp[?]id=[0-9]*",
				"http://edu[.]cnhubei[.]com/[?]action-viewnews-itemid-[0-9]*",
				"http://travel[.]cnhubei[.]com/portal[.]php[?]mod=view&aid=[0-9]*",
				"http://xy[.]cnhubei[.]com/[0-9]{4}/[0-9]{4}/[0-9]*.shtml",
				"http://yc[.]cnhubei[.]com/html/.*/[0-9]{8}-[0-9]*.html"};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		String html=urlMeta.getHtml();
		String title="";
		String content="";
		String author=null;
		Long date=0l;
		String source="荆楚网";
		int commentNum=0;
		int clickNum=0;
		String url=urlMeta.getUrl();
		NewsMeta meta=new NewsMeta();
		meta.setUrl(urlMeta.getUrl());
		meta.setType(0);
		Document doc=Jsoup.parse(html);
		author=doc.select("a#source_url").text();
		if(StringUtils.isBlank(author)){
			author=doc.select("div#ct.ct2 div.mn div.bm div.h p.xg1 a").text();
		}
		/*
		 * 解析新闻标题
		 */

		title=doc.select("div.Title>strong").text();
		if (StringUtils.isBlank(title)) {
			Elements titleElement=doc.select("h1");
			if (titleElement.size() > 0) {
				title=titleElement.get(0).ownText();
			}
			if (StringUtils.isBlank(title)) {
				title=doc.select("title").text();
				int pos=title.indexOf("-");
				if(pos!=-1){
					title=title.substring(0,pos);
				}
			}
		}

		/*
		 * 解析时间
		 */
		date=DateUtils.matchDate(doc.select("div.C_1_2_1,div.mintitle").text());
		if (date == null) {
			date=DateUtils.matchDate(doc.select("div.page_Info").text());
			if (date == null) {
				date=DateUtils.matchDate(doc.select("div.Info").text());
				if (date == null) {
					date=DateUtils.matchDate(doc.select("font.menu").text());
					if (date == null) {
						date=DateUtils.matchDate(doc.select("p.xg1").text());
						if (date == null) {
							date=DateUtils.matchDate(doc.select("div#Article>h1").text());
							if (date == null) {
								date=DateUtils.matchDate(doc.select("div.page_Info_xin").text());
								if (date == null) {
									date=JSoupUtils.matchDate(doc, "来源：");
								}
							}
						}
					}
				}
			}
		}




		/*
		 * 解析内容
		 */
		content=HtmlCleaner.getContentHtml(url,doc.select("div#newscontent"));
		if (StringUtils.isBlank(content)) {
			content=HtmlCleaner.getContentHtml(url,doc.select("div.page_Content"));
		}
		if (StringUtils.isBlank(content)) {
			content=HtmlCleaner.getContentHtml(url,doc.select("div.Content"));
		}
		if (StringUtils.isBlank(content)) {
			content=HtmlCleaner.getContentHtml(url,doc.select("td#content"));
		}
		if (StringUtils.isBlank(content)) {
			content=HtmlCleaner.getContentHtml(url,doc.select("td#article_content"));
		}
		if (StringUtils.isBlank(content)) {
			content=HtmlCleaner.getContentHtml(url,doc.select("div.page_Content_xin"));
		}

		meta.setTitle(title);
		meta.setContent(content);
		meta.setDate(date);
		meta.setAuthor(author);
		meta.setClickNum(clickNum);
		meta.setCommentNum(commentNum);

		return meta;
	}

	public static void main(String[] args) {
		String url="http://news.cnhubei.com/xw/gn/201409/t3042059.shtml";
		UrlMeta urlMeta=CrawlHTML.responseToURL(url);
		CnhubeiNewsAnalyse cnhubeiNewsAnalyse=new CnhubeiNewsAnalyse();
		boolean detailPage=cnhubeiNewsAnalyse.isDetailPage(url);
		if (detailPage) {
			NewsMeta parserHtml=new CnhubeiNewsAnalyse().parserHtml(urlMeta);
			System.out.println(parserHtml);
		} else {
			System.out.println("不符合正则");
		}
	}

	
	public NewsMeta Update(NewsMeta meta) {
		/**
		 * 网站本身没有评论，点击数
		 */
		return meta;
	}
	
	public boolean isNeedUpdate(){
		return true;
	}
}
